package cn.sym.org.service.impl;

import cn.sym.basic.exception.MyException;
import cn.sym.basic.service.impl.BaseServiceImpl;
import cn.sym.basic.util.BaiduAuditUtils;
import cn.sym.org.domain.Employee;
import cn.sym.org.domain.Shop;
import cn.sym.org.domain.ShopAuditLog;
import cn.sym.org.mapper.EmployeeMapper;
import cn.sym.org.mapper.ShopAuditLogMapper;
import cn.sym.org.mapper.ShopMapper;
import cn.sym.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sym
 * @since 2022-02-11
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        System.out.println(shop);
        if (!StringUtils.hasLength(shop.getName())){
            throw new MyException("店铺名不能为空！！！");
        }
        //使用百度AI检测店铺名是否合法
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new MyException("店铺名不合法，请换一个再试！！！");
        }
       // BaiduAuditUtils.getAuth();
        if (!BaiduAuditUtils.ImgCensor(shop.getLogo())){
            System.out.println(shop.getLogo());
            throw new MyException("店招logo不合法，请换一个再试！！！");
        }
        //根据用户传入的店铺名去查找数据库，如果店铺名已经存在
        Shop shopName = shopMapper.loadByName(shop.getName());
        if (shopName!=null){
            //当店铺名字不为空时，提示用户该店铺名字已经存在，那么就只需要登录
            throw new MyException("该店铺名已经被注册！！,请直接登录");
            //如果忘记密码，那么就找回密码
        }
        //先获取到用户输入的管理员信息     private Employee admin;
        Employee admin = shop.getAdmin();
        //调用employeeMapper接口方法检查employee是否曾今注册过了
        Employee employee = employeeMapper.loadByUserName(admin.getUsername());
        //如果不存在此employee
        if (employee==null){
            //新增此employee
            employeeMapper.add(admin); //新增之后会返回自增长的id
            //把admin赋值给中间变量，也就是说employee里面也有了id
            employee = admin;
        }
        //shop表里面的admin_id就是employee表里面的id
        shop.setAdmin_id(employee.getId());
        if (shop.getId()!=null){
            shopMapper.update(shop);
        }else {
            shopMapper.add(shop);
        }
        //获取到employee返回的id，把id作为employee的外键设置值
        employee.setShop_id(shop.getId());
        employeeMapper.update(employee);

    }
    /**
     * 驳回之后点击链接修改信息
     * @param shop
     */
    @Override
    public void settlement2(Shop shop) {
        if (!StringUtils.hasLength(shop.getName())){
            throw new MyException("店铺名不能为空！！！");
        }
        //使用百度AI检测店铺名是否合法
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new MyException("店铺名不合法，请换一个再试！！！");
        }
        //通过用户输入的修改后的店铺名去数据库查找是否已经存在有此店铺名的数据
        Shop shop2 = shopMapper.loadByName(shop.getName());
        //前提条件是查询得到的店铺信息不为空
        if (shop2!=null){
            //如果用户修改的数据的id与数据库通过店铺名查询到的店铺id相同，说明此店铺名只存在自己本身一个，才能进行修改，否则就是说此店铺名已经存在了，就不能再使用这个店铺名了
            if (shop.getId()==shop2.getId()){
                shopMapper.update(shop);
            }else{
                throw new MyException("已存在相同店铺名，请换一个再试！！！");
            }
        }
    }
    /**
     * 导入数据，批量添加
     * @param list
     */
    @Override
    public void addMore(List<Shop> list) {
        shopMapper.addMore(list);
    }

    /**
     * 审核通过
     * @param shopAuditLog
     */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        //1.修改店铺状态为待激活
        Long shopId = shopAuditLog.getShop_id();
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(1);
        shopMapper.update(shop);
        //2.记录审核日志
        //设置当前登录人，由于我们现在没有，直接写死，后面来改
        shopAuditLog.setAudit_id(334L);
        shopAuditLogMapper.add(shopAuditLog);
        //3.发送激活邮件或短信
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("1149813304@qq.com");
        message.setTo("1149813304@qq.com");
        message.setSubject("通过");
        message.setText("美女的事你少管");
        javaMailSender.send(message);
    }

    /**
     * 人工审核驳回
     * @param shopAuditLog
     */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        //1.修改店铺状态为待激活
        //首先获取到这个店铺的id
        Long shopId = shopAuditLog.getShop_id();
        //通过店铺的id把这个店铺的信息查询出来
        Shop shop = shopMapper.loadById(shopId);
        //修改这个店铺的状态为通过
        shop.setState(-1);
        //最后把这个店铺更新
        shopMapper.update(shop);
        //2.记录审核日志
        //设置当前登录人，由于我们现在没有，直接写死，后面来改
        shopAuditLog.setAudit_id(334L);
        shopAuditLogMapper.add(shopAuditLog);
        //店铺关联的管理员
        Employee manager = employeeMapper.loadById(shop.getAdmin_id());
        //3.发送邮件通知 商家 审核失败，告知原因，修改的地址
        //复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            //使用工具类来构建消息
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            //发邮件人
            helper.setFrom("1149813304@qq.com");
            //设置邮件主题
            helper.setSubject("审核失败通知");
            //邮件正文
            helper.setText("<h1>审核失败</h1><br>"+
                    "<span>失败原因："+shopAuditLog.getNote()+"</span><br>"+
                    "<a href='http://127.0.0.1:8081/#/register2?id="+shop.getId()+"'>点击修改资料</a>",true);
            //接收邮件人
            helper.setTo("1149813304@qq.com");
            //发送消息
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }


}
